<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;
use Storage;


/**
 * Class Zixun
 * @package App
 */
class Zixun extends Model
{
    public $timestamps = false;
    protected $table = 'kl_column_dic';

    //是否可以回复
    const DEL_ON = 1;
    const DEL_OFF = 0;
	
    static function PostingsCols()
    {
        return DB::table('kl_column_dic')->select('id', 'column_name')->orderByDesc('id')->where('is_del', self::DEL_ON)->get()->toArray();
    }
	//刷新
	static function Shuaxin($id, $request){
		DB::beginTransaction();

        if (!Zixun::where('id', $id)->update([
            'create_time' => date('Y-m-d H:i:s',time())
        ])) {
            DB::rollBack();

            return ['status' => 2, 'message' => '刷新失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '刷新成功'];
	}
       /**
     * 删除
     * @param $c_id
     * @param $request
     * @return array
     * @throws \Exception
     */
    static function Dels($id, $request)
    {
        DB::beginTransaction();
		
        try {
            if (!DB::table('kl_column_dic')->where('id', $id)->delete()) {
				DB::rollBack();
                return ['message' => '删除失败', 'status' => 2];
            }
			
		} catch (Exception $exception) {
			return ['message' => '删除失败', 'status' => 2];
		}
		if(DB::table('kl_rel_user_column')->select('id')->where('id', '>','0')->first()){
			if (!DB::table('kl_rel_user_column')->where('id','>','0')->delete()) {
				DB::rollBack();
				return ['message' => '分类删除失败', 'status' => 2];
			}	
		}	

		
        DB::commit();
 
        return ['status' => 1, 'message' => '删除成功'];		
        //添加系统消息
        //$message = Curl::PushOneMessage($c_id, $request->ban_reason);
        //if ($message['status'] !== 1) {
        //    DB::rollBack();

        //    return $message;
        //}
//        if (!DB::table('jpush')->insert(
//            [
//                'title' => '系统消息',
//                'content' => $request->ban_reason,
//                'ctime' => date('Y-m-d H:i:s', time()),
//                'user_id' => $c_id
//            ]
//        )) {
//            DB::rollBack();
//
//            return ['status' => 2, 'message' => '发送系统消息失败'];
//        }
//
//        if (!Curl::PushNum($c_id)) {
//            DB::rollBack();
//
//            return ['status' => 2, 'message' => '发送系统消息失败'];
//        }
        //推送
        //如果关联了公众号 
//        if ($open_id = User::where('c_id', $c_id)->value('wx_open_id')) {
//
//            $message = new Text($request->ban_reason);
//            $app = Wechat::OfficialConfig();
//            $result = $app->customer_service->message($message)->to($open_id)->send();
////            if ($result['errcode'] !== 0 || $result['errmsg'] !== 'ok') {
////                DB::rollBack();
////
////                return ['status' => 2, 'message' => $result['errmsg']];
////            }
//        }

//        $a = Curl::GetCurl(Curl::CURL_URI . Curl::CURL_ORDERPUSH, ['alias' => $c_id, 'content' => $request->ban_reason, 'pcs' => 3]);

    }
	
    static function FenleiModify($request, $zixun)
    {
		
        $zixun->column_name = $request->column_name;
        if (!$zixun->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        if (!DB::table('kl_rel_user_column')->where('id','>',0)->delete()) {
			DB::rollBack();
            return ['message' => '信息删除失败', 'status' => 2];
        }
        return ['status' => 1, 'message' => '成功'];

    }

    /**
     * 添加分类
     * @param $request
     * @return array
     */
    static function FenleiPublish($request)
    {
        $zixun = new Zixun();
        $zixun->column_name = $request->column_name;
       
        $zixun->create_time = date('Y-m-d H:i:s', time());
        
        if (!$zixun->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        if (!DB::table('kl_rel_user_column')->where('id','>',0)->delete()) {
			DB::rollBack();
            return ['message' => '信息删除失败', 'status' => 2];
        }
        return ['status' => 1, 'message' => '成功'];

    }
}
